package com.maomao.maomaochong_i.personalPackage;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.maomao.maomaochong_i.MainActivity;
import com.maomao.maomaochong_i.R;
import com.maomao.maomaochong_i.adapterPackage.MaoGridAdapter;
import com.maomao.maomaochong_i.application.MyApplication;
import com.maomao.maomaochong_i.dataBean.AllItem;
import com.maomao.maomaochong_i.dataBean.MySharePreference;

import java.util.ArrayList;

/**
 * Created by maomao on 2017/5/17.
 * 个人页
 */

public class PersonalPage {
    private Context context;
    private Intent intent;
    private MySharePreference msp;
    private View personalView;
    private CircularView circular;
    private TextView useId;
    private ImageView setImageView;
    private Button loginBut, orderBut1, orderBut2;
    private TextView textView1, textView2, textView3, textView4;
    private GridView grid;
    private BaseAdapter mAdapter = null;
    private ArrayList<AllItem> mData = null;
    private int[] imageId = {R.mipmap.fourth1,R.mipmap.fourth2,R.mipmap.fourth3,R.mipmap.fourth4,
            R.mipmap.fourth5,0,0,0,0,0,0,0};
    private String[] name = {"收藏","意见反馈","分享","关于","退出","","","","","","",""};

    private boolean is_login;
    private String userName = null;

    //默认构造方法
    public PersonalPage() {}

    //调用方法
    public View usePersonal(Context context, LayoutInflater inflater, ViewGroup container){
        this.context = context;
        //获得 XML布局
        personalView = inflater.inflate(R.layout.activity_personal, container, false);

        //获得布局中的组件
        bindView();

        msp = new MySharePreference(context);
        is_login = msp.getData("state",false);

        if (is_login == true){
            //隐藏登录按钮
            loginBut.setVisibility(View.INVISIBLE);
            //显示头像和id
            circular.setVisibility(View.VISIBLE);
            useId.setVisibility(View.VISIBLE);

            // !!!!  登录之后获得 bmob 的用户名和头像
            // userName = ???;
            // image = ???;

            Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.img);
            this.circular.setImageBitmap(bitmap);

            //如果用户名为空
            if (userName == null){
                //默认id
                useId.setText("编号:54088");
            } else {
                //如果有用户名，显示出来
                useId.setText(userName);
            }
        }

        //返回视图
        return personalView;
    }

    private void bindView(){
        circular = (CircularView) personalView.findViewById(R.id.circularView);
        useId = (TextView) personalView.findViewById(R.id.use_id);
        setImageView = (ImageView) personalView.findViewById(R.id.imageButton1);
        loginBut = (Button) personalView.findViewById(R.id.loginBut1);
        orderBut1 = (Button) personalView.findViewById(R.id.button1);
        orderBut2 = (Button) personalView.findViewById(R.id.button2);
        textView1 = (TextView) personalView.findViewById(R.id.textView1);
        textView2 = (TextView) personalView.findViewById(R.id.textView2);
        textView3 = (TextView) personalView.findViewById(R.id.textView3);
        textView4 = (TextView) personalView.findViewById(R.id.textView4);


        //给上面组件设置监听
        circular.setOnClickListener(listener);
        setImageView.setOnClickListener(listener);
        loginBut.setOnClickListener(listener);
        orderBut1.setOnClickListener(listener);
        orderBut2.setOnClickListener(listener);
        textView1.setOnClickListener(listener);
        textView2.setOnClickListener(listener);
        textView3.setOnClickListener(listener);
        textView4.setOnClickListener(listener);


        //获得 GridView组件并设置适配监听
        grid = (GridView) personalView.findViewById(R.id.gridView);
        mData = new ArrayList<AllItem>();
        for (int i = 0; i < imageId.length; i++) {
            mData.add(new AllItem(imageId[i],name[i]));
        }

        mAdapter = new MaoGridAdapter<AllItem>(mData,R.layout.personal_grid_item) {
            public void bindView(gridViewHolder holder, AllItem obj){
                holder.setImageResource(R.id.gridImage, obj.getId());
                holder.setText(R.id.gridText, obj.getName());
            }
        };
        grid.setAdapter(mAdapter);
        grid.setOnItemClickListener(itemListener);
    }

    private View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.imageButton1:
                    if (is_login == true){
                        intent = new Intent(context,PersonalSet.class);
                        context.startActivity(intent);
                    }else {
                        affirmLogin();
                    }

                    break;

                case R.id.loginBut1:
                    intent = new Intent(context,Login.class);
                    context.startActivity(intent);

                    break;

                case R.id.button1:
                    if (is_login == false){
                        affirmLogin();
                    }else {
                        //如果没内容
                        allpage();
                    }
                    break;

                case R.id.button2:
                    if (is_login == false){
                        affirmLogin();
                    }else {
                        //如果没内容
                        allpage();
                    }
                    break;

                case R.id.textView1:
                    if (is_login == false){
                        affirmLogin();
                    }else {
                        //如果没内容
                        allpage();
                    }
                    break;

                case R.id.textView2:
                    if (is_login == false){
                        affirmLogin();
                    }else {
                        //如果没内容
                        allpage();
                    }
                    break;

                case R.id.textView3:
                    if (is_login == false){
                        affirmLogin();
                    }else {
                        //如果没内容
                        allpage();
                    }
                    break;

                case R.id.textView4:
                    if (is_login == false){
                        affirmLogin();
                    }else {
                        //如果没内容
                        allpage();
                    }
                    break;

                case R.id.circularView:
                    intent = new Intent(context,PensonalMessage.class);
                    context.startActivity(intent);
                    break;
            }
        }
    };

    private AdapterView.OnItemClickListener itemListener = new AdapterView.OnItemClickListener(){
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id){
            switch (position){
                case 0:
                    if (is_login == false){
                        affirmLogin();
                    }else {
                        //如果没内容
                        allpage();
                    }
                    break;

                case 1:
                    if (is_login == false){
                        affirmLogin();
                    }else {
                        //如果没内容
                        allpage();
                    }
                    break;

                case 2:
                    if (is_login == false){
                        affirmLogin();
                    }else {
                        //如果没内容
                        allpage();
                    }
                    break;

                case 3:
                    if (is_login == false){
                        affirmLogin();
                    }else {
                        //如果没内容
                        allpage();
                    }
                    break;

                case 4:
                    MyApplication.getInstance().exit();
                    break;

                case 5:

                    break;

                case 6:

                    break;

                case 7:

                    break;

                case 8:

                    break;
            }
        }
    };

    private void affirmLogin(){
        Toast.makeText(context,"请先登录账户",Toast.LENGTH_SHORT).show();
        intent = new Intent(context,Login.class);
        context.startActivity(intent);
    }

    private void allpage(){
        intent = new Intent(context,TryActivity.class);
        context.startActivity(intent);
    }
}

